package com.app.framework.Security;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.session.SessionRegistry;

public class SecurityUtils {

	@Autowired
	private SessionRegistry sessionRegistry;// 这个类会自动注入 不用我们自己去手动注入

	public static String getCurrentUser() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		String currentPrincipalName = authentication.getName();

		return currentPrincipalName;
	}

	/**
	 * 获取当前用户
	 * 
	 * @return
	 */
	public static Authentication getCurrentUserAuthentication() {
		return SecurityContextHolder.getContext().getAuthentication();

	}

	/**
	 * 获取当前用户
	 * 
	 * @return
	 */
	public static Object getCurrentPrincipal() {
		return getCurrentUserAuthentication().getPrincipal();
	}

	public void getAllLoginUsers() {

		System.out.println("u begin");

		List<Object> allPrincipals = sessionRegistry.getAllPrincipals();
		for (int i = 0, len = allPrincipals.size(); i < len; i++) {
			System.out.println(allPrincipals.get(i));// 可以转换成spring的User

		}
		System.out.println("u end");
	}
	public static boolean isPermitted(String permission) {

		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		String currentPrincipalName = authentication.getName();
		
		return true;
	}
	 
}
